
function loader(source) {
  const reg = /url\((.+?)\)/g
  let pos = 0
  let current
  const arr= ['const list = []']
  while (current = reg.exec(source)) { // [matchUrl, g]
    const [matchUrl, g] = current
    const last = reg.lastIndex - matchUrl.length // current.index
    // console.log(matchUrl, g);
    arr.push(`list.push(${JSON.stringify(source.slice(pos, last))})`)
    pos = reg.lastIndex
    // 把 g 替换成 require 的写法
    arr.push(`list.push('url('+ require(${g}) +')')`)

  }
  arr.push(`list.push(${JSON.stringify(source.slice(pos))})`)
  arr.push(`module.exports = list.join('')`)
  // console.log(arr.join('\r\n'));

  // return source
  return arr.join('\r\n')
}

module.exports = loader